<?php
class GroupAction extends Action
{
	public function myGroup()
	{
		$id = $_SESSION['id'];
		$student_dao = M('student');
		$group_dao = M('group');
		$condition['id'] = $id;
		$result = $student_dao ->where($condition)->find();
		$student = $result;
		$student_name = $result['name'];
		$student_id = $result['studentid'];
		$groupid = $result['group_id'];		
		$condition2['group_id'] = $groupid;
		$result = $student_dao ->where($condition2)->select();
		
		$condition3['id'] = $groupid;
		$result2 = $group_dao ->where($condition3)->find();
		$leaderid = $result2['leader_id'];
		
		$leader;

		foreach ($result as $key => $value ) {
			if($result[$key]['id'] == $leaderid){
				$leader = $result[$key];
				//array_slice($result, $key, 1);
				unset($result[$key]);
				break;
			}
		}
		$this->assign('members', $result );
		$this->assign('leader', $leader);	
		$this->assign('group', $result2);
		$this->assign('student', $student);

        $this->display();
	}
	
	public function allGroups()
	{
		$group_dao = M('group');
		$student_dao = M('student');
		$result = $group_dao->select();
		$condition['id'] = $_SESSION['id'];
		$student = $student_dao->where($condition)->find();
		foreach ($result as $key => $value) {
			$leaderid = $value['leader_id'];
			$condition['id'] = $leaderid;
			$leader = $student_dao->where($condition)->find();
			$result[$key]['leadername'] = $leader['name'];
	    }
		$this->assign('groups', $result);
		$this->assign('student',$student);
		
		$this->display();
	}
	public function add()
	{
		$group_dao  = M('group');
		$student_dao = M('student');
		$group_check['id']=$_SESSION['id'];
		$group_result = $student_dao->where($group_check)->find();
		if($group_result['group_id'] != NULL){
			//$this->error('您已经是某小组的成员！');
			//$this->redirect('Group/myGroup');
			$this->ajaxReturn(null, null, 0);
		}
		
		$data['leader_id'] = $_SESSION['id'];
		$data['name'] = $_POST['groupname'];
		$data['create_time'] = time();
		if($lastInsId = $group_dao ->add($data)){
			$condition['id'] = $_SESSION['id'];
			$studata['group_id'] = $lastInsId;
			$result = $student_dao->where($condition)->data($studata)->save();
			if($result !== false){
				//$this->redirect('Group/myGroup');
				$this->ajaxReturn(null,null,1);
			}
			else{
				$this->ajaxReturn(null, null, 0);
				
			}
			
        
	    } else {
	        $this->ajaxReturn(null, null, 0);
	    }
	}
	public function addGroup()
	{
		
		
	}

	
	public function groupPage()
	{
		$student_dao = M('student');
		$group_dao = M('group');
	    $groupid = $_GET['group_id'];
		$condition2['group_id'] = $groupid;
		$result = $student_dao ->where($condition2)->select();
		
		$condition3['id'] = $groupid;
		$result2 = $group_dao ->where($condition3)->find();
		$leaderid = $result2['leader_id'];
		
		$leader;

		foreach ($result as $key => $value ) {
			if($result[$key]['id'] == $leaderid){
				$leader = $result[$key];
				$result = array_slice($result, $key + 1);
				break;
			}
		}
		$this->assign('members', $result );
		$this->assign('leader', $leader);	
		$this->assign('group', $result2);
		
        $this->display();
	}
	public function join(){
		$groupid = $_GET['group_id'];
	    $student_dao = M('student');
		$condition['id'] = $_SESSION['id']; 
		$result = $student_dao->where($condition)->find();
		if($result['group_id'] == NULL)
		{
			$data['group_id'] = $groupid;
			$result = $student_dao->where($condition)->save($data);
			$this->ajaxReturn(null, null, 1);
		}
		else{
			$this->ajaxReturn(null, null, 0);
		}
	}
	public function drop(){
		$id = $_SESSION['id'];
		$group_id = $_GET['group_id'];
		$condition['id'] = $id;
		$student_dao = M('student');
		$data['group_id'] = NULL;
		$student_dao->where($condition)->save($data);
		$condition2['group_id'] = $group_id;
		$result = $student_dao->where($condition2)->select();
		if($result == null){
			$condition3['id'] = $group_id;
			$group_dao = M('group');
			$group_dao->where($condition3)->delete();
		}
		$this->ajaxReturn(null, null, 1);
	}
}

?>